from django.db.models import F
from django.shortcuts import render, HttpResponseRedirect
from django.utils import timezone

from login.models import *


# Create your views here.
def index(request):
    media = Media.objects.filter(is_active=True)
    return render(request, 'user/index.html', {"mm": media})


def media_add(request):
    if request.method == "GET":
        return render(request, 'user/media_add.html')
    if request.method == "POST":
        name = request.session.get('name')
        user = User.objects.get(name=name)
        # print(user)
        title = request.POST['title']
        synopsis = request.POST['synopsis']
        photo = request.FILES.get('photo')
        # try:
        media = Media.objects.create(user=user, title=title, synopsis=synopsis, photo=photo)
        # except Exception as e:
        #     print('user_media_add:', e)
        return HttpResponseRedirect('/user/my_media/')


def vote(request, id):
    """

    1.MyVote(models.Model):
        media = models.ForeignKey(verbose_name="资源", to='Media', on_delete=models.CASCADE)
        user = models.ForeignKey(verbose_name="用户", to='User', on_delete=models.CASCADE)
        time = models.DateTimeField(verbose_name="投票时间", auto_now=True)
        is_active = models.BooleanField('是否活跃', default=True)
    """
    user = request.session.get('name')
    try:
        media = Media.objects.get(id=id, is_active=True)
        user = User.objects.get(name=user)
        myvote = MyVote.objects.filter(user=user, media=media)
    except Exception as e:
        print('user_vote:', e)
    if myvote:
        time = myvote.get(media=media)
        d1 = timezone.now()
        if time.time.day == d1.day:
            msg = "你今天已投票，明天再来！"
            return render(request, 'user/msg.html', {"msg": msg})
            # return HttpResponse(msg)
        else:
            print(time.time)
            time.vote = F("vote") + 1
            time.save()
            return HttpResponseRedirect('/user/')



    else:
        myvote = MyVote.objects.create(user=user, media=media)
        media.vote = F("vote") + 1
        media.save()
        return HttpResponseRedirect('/user/')


def my_media(request):
    user = request.session.get('name')
    try:

        user = User.objects.get(name=user)
        media = Media.objects.filter(user=user, is_active=True)
    except Exception as e:
        print('user_vote:', e)
    return render(request, 'user/my_media.html', {"media": media})


def media_edit(request, id):
    try:
        media = Media.objects.get(id=id, is_active=True)
    except Exception as e:
        print(e)
        msg = "没有此评价内容："
        return render(request, 'user/msg.html', {"msg": msg})
    if request.method == 'GET':
        return render(request, 'user/media_edit.html', {"media": media})
    elif request.method == 'POST':
        title = request.POST['title']
        synopsis = request.POST['synopsis']
        photo = request.FILES.get('photo')
        media.title = title
        media.synopsis = synopsis
        media.photo = photo
        media.save()
        return HttpResponseRedirect('/user/my_media/')


def media_del(request, id):
    try:
        media = Media.objects.get(id=id, is_active=True)
    except Exception as e:
        print(e)
        msg = "没有此评价内容："
        return render(request, 'user/msg.html', {"msg": msg})
    media.is_active = False
    media.save()
    return HttpResponseRedirect('/user/my_media/')


def media_ranking(request):
    try:
        media = Media.objects.filter(is_active=True).order_by('vote')
    except Exception as e:
        print('user_vote:', e)
    return render(request, 'user/media_ranking.html', {"media": media})
